<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Reviews extends MX_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model(array('admin_commoncall','admin_settings_model','admin_review_model','admin_member_model','admin_listing_model','admin_counter_model'));
		$this->load->library('pagination');
		$this->admin_commoncall->check_admin_login();
		$this->template->set_theme('admin');
		$this->template->set_layout('default');
		//$this->template->set_partial('side_menu','layouts/side_menu');
		//$this->template->set_partial('nav_bar','layouts/nav_bar');

	}

	function index()
	{
		//-- Default Value --//
		$status=1;
		$featured=false;
		$search = "";

		if($this->input->post('filter')){
			$filter = $this->input->post('filter');

			switch ($filter) {
				case 'active':
					$status = 51;
					break;
				case 'inactive':
					$status = 1;
					break;
				case 'featured':
					$status = 51;
					$featured=true;
					break;
			}
		}

		$offset = 0 + $this->uri->segment(5);
		
		$total = $this->admin_counter_model->count_all_reviews('review_master');

		//print "$total";
		//die();

		$perpage = 5;
		$config['per_page'] = $perpage;
		$config['total_rows'] = $total;
		$config['uri_segment'] = '5';

		$config['base_url'] = base_url() . 'secure/approval/reviews/active/';

		$cur =base_url()."secure/approval/reviews/active/";
		$cur = '<a href="'. $cur   .'">&laquo; First</a>';
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';
		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page" style="display:inline">';
		$config['last_tag_close'] = '</li>';
		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page" style="display:inline">';
		$config['next_tag_close'] = '</li>';
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		$config['display_pages'] = FALSE;
		
		
		
		if($status==51)
		{
			$data['review']['all'] = $this->admin_review_model->get_reviews($featured = $featured, $status = $status, $perpage, $offset);
				
				
			$this->pagination->initialize($config);
		}else{

			$status=1;
			$offset = 0 + $this->uri->segment(5);
			
			$total = $this->admin_counter_model->count_inactive_reviews($status);

			//print "$total";
			//die();

			$perpage = 5;
			$config['per_page'] = $perpage;
			$config['total_rows'] = $total;
			$config['uri_segment'] = '5';

			$config['base_url'] = base_url() . 'secure/approval/reviews/index/';
			
			$cur =base_url()."secure/approval/reviews/index/";
			$cur = '<a href="'. $cur   .'">&laquo; First</a>';
			$config['full_tag_open'] = '<div class="pagination"><ul>';
			$config['full_tag_close'] = '</ul></div><!--pagination-->';
			$config['first_link'] = '&laquo; First';
			$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['first_tag_close'] = '</li>';
			$config['last_link'] = 'Last &raquo;';
			$config['last_tag_open'] = '<li class="next page" style="display:inline">';
			$config['last_tag_close'] = '</li>';
			$config['next_link'] = 'Next &rarr;';
			$config['next_tag_open'] = '<li class="next page" style="display:inline">';
			$config['next_tag_close'] = '</li>';
			$config['prev_link'] = '&larr; Previous';
			$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['prev_tag_close'] = '</li>';
			$config['cur_tag_open'] = '<li class="active"><a href="">';
			$config['cur_tag_close'] = '</a></li>';
			$config['num_tag_open'] = '<li class="page">';
			$config['num_tag_close'] = '</li>';
			$config['display_pages'] = FALSE;
			
			
			$data['review']['all'] = $this->admin_review_model->get_inactive_reviews($featured = $featured, $status = $status, $perpage, $offset);
				
			$this->pagination->initialize($config);
		}

		//$data['review']['all'] = $this->admin_review_model->get_reviews($featured = false, $status = $status, $no = 30, $offset = NULL);

		if($this->input->post('search')){
			$search = 	trim($this->input->post('search'));
			$data['review']['all'] = $this->admin_review_model->search($search = $search);
			//print $search;
		}


		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('approval/reviews',$data);
	}
	
	function active()
	{
		
		//-- Default Value --//
		$status=51;
		$featured=false;
		$search = "";

		if($this->input->post('filter')){
			$filter = $this->input->post('filter');

			switch ($filter) {
				case 'active':
					$status = 51;
					break;
				case 'inactive':
					$status = 1;
					break;
				case 'featured':
					$status = 51;
					$featured=true;
					break;
			}
		}

		$status=51;
		
		$offset = 0 + $this->uri->segment(5);
		
		$total = $this->admin_counter_model->count_inactive_reviews($status);

		//print "$total";
		//die();

		$perpage = 5;
		$config['per_page'] = $perpage;
		$config['total_rows'] = $total;
		$config['uri_segment'] = '5';

		$config['base_url'] = base_url() . 'secure/approval/reviews/active/';
		
		$cur =base_url()."secure/approval/reviews/active/";
		$cur = '<a href="'. $cur   .'">&laquo; First</a>';
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';
		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page" style="display:inline">';
		$config['last_tag_close'] = '</li>';
		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page" style="display:inline">';
		$config['next_tag_close'] = '</li>';
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		$config['display_pages'] = FALSE;
		$data['review']['all'] = $this->admin_review_model->get_reviews($featured = $featured, $status = $status, $perpage, $offset);
				
		$this->pagination->initialize($config);
		
		if($this->input->post('search')){
			$search = 	trim($this->input->post('search'));
			$data['review']['all'] = $this->admin_review_model->search($search = $search);
			//print $search;
		}

		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('approval/reviews',$data);
	}
	
	

	function change_reviews_status($status=NULL,$id=NULL){
		$this->admin_review_model->change_status($id,$status);
		redirect('secure/approval/reviews');
	}
	function delete_reviews($id){
		$this->admin_review_model->delete($id);
		redirect('secure/approval/reviews');
	}

	
	function edit($id){

		//$data['mode'] = "EDIT";

		$data['review']['details'] = $this->admin_review_model->get_review($id);
		$data['id'] = $id;
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		->build('approval/review_add',$data);
	}

	function update()
	{

		$this->form_validation->set_rules('name','Name','trim|xss_clean|required');
		$this->form_validation->set_rules('email','Email','trim|xss_clean|required|valid_email');
		$this->form_validation->set_rules('mobile','Mobile Number','trim|xss_clean|required|numeric');
		$this->form_validation->set_rules('review_details','Comment','trim|xss_clean|required');

		if($this->form_validation->run() == true){
			$id = $this->input->post('id');
			$post_data=array(

					'name' => $this->input->post('name'),
					'email' => $this->input->post('email'),
					'mobile' => $this->input->post('mobile'),
					'comment' => $this->input->post('review_details'),

			);

			$this->db->where('id',$id);
			$this->db->update('review_master',$post_data);
			$msg = "<span class='error'><strong>Review Updated Successfully</strong> </span>";
			$this->session->set_flashdata('message', $msg);
			redirect('secure/approval/reviews/edit/'.$this->input->post('id'));
		}else{
				
			$msg = "<span class='error'><strong>Fields Can't be Null</strong> </span>";
			$this->session->set_flashdata('message', $msg);
			redirect('secure/approval/reviews/edit/'.$this->input->post('id'));


		}

	}
	//--test--//

}